<html>
<head><meta charset="utf-8"><title>ptr::align_offset and slice::align_to · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ptr.3A.3Aalign_offset.20and.20slice.3A.3Aalign_to.html">ptr::align_offset and slice::align_to</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234829551"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ptr%3A%3Aalign_offset%20and%20slice%3A%3Aalign_to/near/234829551" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ptr.3A.3Aalign_offset.20and.20slice.3A.3Aalign_to.html#234829551">(Apr 16 2021 at 10:26)</a>:</h4>
<p>Why these functions have pretty weak guarantees about their return value?<br>
It's kinda weird, because the actual implementation does what everyone expects<br>
<a href="https://github.com/Lokathor/bytemuck/issues/19">An example issue that it causes</a></p>



<a name="234829772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ptr%3A%3Aalign_offset%20and%20slice%3A%3Aalign_to/near/234829772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Soveu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ptr.3A.3Aalign_offset.20and.20slice.3A.3Aalign_to.html#234829772">(Apr 16 2021 at 10:28)</a>:</h4>
<p>slice::align_to</p>
<div class="codehilite"><pre><span></span><code>The method may make the middle slice the greatest length possible for a given type and input slice,
but only your algorithm’s performance should depend on that, not its correctness.
It is permissible for all of the input data to be returned as the prefix or suffix slice.
</code></pre></div>
<p>ptr::align_offset</p>
<div class="codehilite"><pre><span></span><code>It is permissible for the implementation to always return usize::MAX.
Only your algorithm’s performance can depend on getting a usable offset here,
not its correctness.
</code></pre></div>



<a name="234830974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ptr%3A%3Aalign_offset%20and%20slice%3A%3Aalign_to/near/234830974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mara <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ptr.3A.3Aalign_offset.20and.20slice.3A.3Aalign_to.html#234830974">(Apr 16 2021 at 10:37)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="124288">@oli</span></p>



<a name="234831773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ptr%3A%3Aalign_offset%20and%20slice%3A%3Aalign_to/near/234831773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ptr.3A.3Aalign_offset.20and.20slice.3A.3Aalign_to.html#234831773">(Apr 16 2021 at 10:44)</a>:</h4>
<p>This is so that const eval can actually interpret function calls to these. We may find other avenues for having different code for CTFE and runtime, at which point we would never need these to be const fn, and thus could change these rules. I would prefer not to though, as it seems better to have the same code for runtime as for ctfe</p>



<a name="234832084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ptr%3A%3Aalign_offset%20and%20slice%3A%3Aalign_to/near/234832084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ptr.3A.3Aalign_offset.20and.20slice.3A.3Aalign_to.html#234832084">(Apr 16 2021 at 10:47)</a>:</h4>
<p>The issue you linked is just a historical miri artifact</p>



<a name="234832108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ptr%3A%3Aalign_offset%20and%20slice%3A%3Aalign_to/near/234832108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ptr.3A.3Aalign_offset.20and.20slice.3A.3Aalign_to.html#234832108">(Apr 16 2021 at 10:47)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="120791">@RalfJ</span></p>



<a name="234832472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ptr%3A%3Aalign_offset%20and%20slice%3A%3Aalign_to/near/234832472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ptr.3A.3Aalign_offset.20and.20slice.3A.3Aalign_to.html#234832472">(Apr 16 2021 at 10:50)</a>:</h4>
<p>The PR that introduced that comment is <a href="https://github.com/rust-lang/rust/issues/63295">#63295</a>, but there's no reason listed there. It would be nice to add a comment for those who end up there after a git blame</p>



<a name="234832648"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ptr%3A%3Aalign_offset%20and%20slice%3A%3Aalign_to/near/234832648" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Giacomo Stevanato <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ptr.3A.3Aalign_offset.20and.20slice.3A.3Aalign_to.html#234832648">(Apr 16 2021 at 10:52)</a>:</h4>
<p>Also, isn't that PR a breaking change? Before unsafe code could have relied on that function always returning the right offset, after it becomes unsound, so it technically "broke" some code.</p>



<a name="234834618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ptr%3A%3Aalign_offset%20and%20slice%3A%3Aalign_to/near/234834618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ptr.3A.3Aalign_offset.20and.20slice.3A.3Aalign_to.html#234834618">(Apr 16 2021 at 11:08)</a>:</h4>
<p>The PR only documented the behaviour, the implementation was always allowed to do that. That is the only reason the function was introduced!</p>



<a name="234903812"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/ptr%3A%3Aalign_offset%20and%20slice%3A%3Aalign_to/near/234903812" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/ptr.3A.3Aalign_offset.20and.20slice.3A.3Aalign_to.html#234903812">(Apr 16 2021 at 18:54)</a>:</h4>
<p><span class="user-mention" data-user-id="328907">@Soveu</span> <a href="https://github.com/rust-lang/rust/issues/62420">#62420</a> goes into more detail</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>